<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav"
                    @titleClick="titleClick"
                    ref="nav"></detail-nav-bar>
    <scroll class="content"
            ref="scroll"
            @scroll="contentScroll"
            :probe-type="3">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo"
                         @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info :paramInfo="paramInfo"
                         ref="params"></detail-param-info>
      <detail-commnet-info :comment-info="commentInfo"
                           ref="comment"></detail-commnet-info>
      <goods-list :goods="recommentds"
                  ref="recomment"></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>

    <back-top @click.native="backClick"
              v-show="isShow"></back-top>

  </div>
</template>

<script>
import Scroll from 'components/common/scroll/Scroll'

import GoodsList from 'components/content/goods/GoodsList'

import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommnetInfo from './childComps/DetailCommnetInfo'
import DetailBottomBar from './childComps/DetailBottomBar'

import { getDetail, Goods, Shop, GoodsParam, getRecommend } from 'network/detail'
import { itemListenerMixin, backTopMixin } from 'common/mixin'

// 映射actions
import { mapActions } from 'vuex'







export default {
  name: 'Detail',
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommentds: [],
      // itemImgListener: null
      themeTopYs: [],
      currentIndex: 0,
    };
  },
  mixins: [itemListenerMixin, backTopMixin],
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommnetInfo,
    GoodsList,
    DetailBottomBar,
  },
  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid

    // 2.根据iid请求详情数据
    getDetail(this.iid).then(res => {
      // console.log(res);

      const data = res.result
      // 1.获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages

      // 2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      // 3.店铺信息对象
      this.shop = new Shop(data.shopInfo)

      // 4.保存商品详情数据
      this.detailInfo = data.detailInfo

      // 5.获取参数的信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      // 6.取出评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }

      // 放在这其中是因为DOM没有值,压根没有渲染,必须这样才能获取到DOM
      // this.$nextTick(() => {
      //   // 根据最新的数据,对应的DOM是已经被渲染出来
      //   // 但是图片依然是没有加载完(目前获取到的offsetTop是不包含其中的图片的)
      //   // offsetTop值不对,一般都是图片产生的原因
      //   this.themeTopYs = []
      //   this.themeTopYs.push(0)
      //   this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.recomment.$el.offsetTop)
      //   console.log(this.themeTopYs);
      // })


    })

    // 3.请求推荐数据
    getRecommend().then(res => {
      this.recommentds = res.data.list
    })
  },
  mounted() {
  },
  updated() {

    // this.themeTopYs = []
    // // 保证一定是有值的
    // this.themeTopYs.push(0)
    // this.themeTopYs.push(this.$refs.params.$el.offsetTop)
    // this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
    // this.themeTopYs.push(this.$refs.recomment.$el.offsetTop)
    // console.log(this.themeTopYs);
  },
  destroyed() {
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  methods: {
    // 映射actions中的函数
    ...mapActions(['addCart']),

    imageLoad() {
      this.$refs.scroll.refresh()

      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop - 49)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 49)
      this.themeTopYs.push(this.$refs.recomment.$el.offsetTop - 49)
      console.log(this.themeTopYs);
    },
    titleClick(index) {
      console.log(index);
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
    },
    contentScroll(position) {
      // 1.获取y值
      const postionY = -position.y

      // 2.positionY和主题中的值进行对比
      // [0,111,222,333]
      // positionY 在 0 和 111 之间 ,index = 0
      // positionY 在 111 和 222 之间 ,index = 1
      // positionY 在 222 和 333 之间 ,index = 2
      // positionY 超过 333 时候 , index = 3
      const length = this.themeTopYs.length
      for (let i = 0; i < length; i++) {
        if (this.currentIndex !== i && (i < length - 1 && postionY >= this.themeTopYs[i] && postionY < this.themeTopYs[i + 1])
          || (i === length - 1 && postionY > this.themeTopYs[i])) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }

      // 是否显示回到顶部
      position.y > -1000 ? this.isShow = false : this.isShow = true


    },
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {}
      product.img = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPirce
      product.iid = this.iid

      // 2.将商品添加到购物车里
      // this.$store.commit('addCart', product)
      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res);
      // }
      // actions可以返回Promise
      this.addCart(product).then(res => {
        this.$toast.shows(res, 2000)
      })
    }

  }
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  height: calc(100% - 44px - 51px);
}
</style>
